package com.usian.comment.service.impl;

import com.usian.comment.feign.ApUserFeign;
import com.usian.comment.service.ApCommentRepayService;
import com.usian.model.comment.dtos.ApCommentRepayVo;
import com.usian.model.comment.dtos.CommentRepayDto;
import com.usian.model.comment.dtos.CommentRepayLikeDto;
import com.usian.model.comment.dtos.CommentRepaySaveDto;
import com.usian.model.comment.pojos.ApComment;
import com.usian.model.comment.pojos.ApCommentRepay;
import com.usian.model.comment.pojos.ApCommentRepayLike;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.user.pojos.ApUser;
import com.usian.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class ApCommentRepayServiceImpl implements ApCommentRepayService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private ApUserFeign apUserFeign;

    @Override/*查看更多回复内容*/
    public ResponseResult loadCommentRepay(CommentRepayDto dto) {

        if (dto.getCommentId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"评论id不存在");
        }

        /*先根据评论id查询*/
        ApComment apComment = mongoTemplate.findById(dto.getCommentId(), ApComment.class);
        if (apComment==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"评论不存在");
        }

        /*查询用户信息*/
        ApUser user = AppThreadLocalUtils.getUser();
        if (user==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"用户不存在");
        }
        ApUser apUser = apUserFeign.findUserById(user.getId());
        if (apUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"用户不存在");
        }
        /*查询评论下面的所有回复   根据评论id和用户id*/
        List<ApCommentRepay> apCommentRepayList =
                mongoTemplate.find(Query.query(Criteria.where("commentId").is(apComment.getId()).and("authorId").is(apUser.getId())), ApCommentRepay.class);

        /*获取所有回复的id*/
        List<String> CommentRepayIdList = apCommentRepayList.stream().map(a -> a.getId()).collect(Collectors.toList());

        /*根据回复的id和用户id查询 点赞表*/
        List<ApCommentRepayLike> commentRepayLikeList = mongoTemplate.find(Query.query(Criteria.where("commentRepayId").in(CommentRepayIdList).and("authorId").is(apUser.getId())), ApCommentRepayLike.class);

        /*用来展示的集合 */
        List<ApCommentRepayVo> list = new ArrayList<>();

        if (commentRepayLikeList.size()!=0 &&  apCommentRepayList.size()!=0) {
            for (ApCommentRepay apCommentRepay : apCommentRepayList) {
                /*先保存ApCommentRepay里面字段 在保存 点赞的字段*/
                ApCommentRepayVo apCommentRepayVo = new ApCommentRepayVo();

                /*对象克隆*/
                BeanUtils.copyProperties(apCommentRepay, apCommentRepayVo);

                for (ApCommentRepayLike apCommentRepayLike : commentRepayLikeList) {
                    if (apCommentRepay.getId().equals(apCommentRepayLike.getCommentRepayId())) {
                        apCommentRepayVo.setOperation((short) 0);
                        break;
                    } else {
                        apCommentRepayVo.setOperation((short) 1);
                        break;
                    }
                }

                list.add(apCommentRepayVo);

            }
            return ResponseResult.okResult(list);
        }else {
            return ResponseResult.okResult(apCommentRepayList);
        }

    }

    @Override/*保存回复*/
    public ResponseResult saveCommentRepay(CommentRepaySaveDto dto) {

        /*检验参数*/
        if (dto.getCommentId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"评论ID不存在");
        }

        /*根据评论id去评论列表查 看看存在不*/
        ApComment apComment = mongoTemplate.findById(dto.getCommentId(), ApComment.class);
        if (apComment==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"评论不存在");
        }

        /*获取用户id*/
        ApUser user = AppThreadLocalUtils.getUser();
        if (user==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"用户不存在");
        }

        /*根据用户id去查询*/
        ApUser apUser = apUserFeign.findUserById(user.getId());
        if (apUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"用户不存在");
        }

        /*保存回复*/
        ApCommentRepay apCommentRepay = new ApCommentRepay();
        apCommentRepay.setAuthorId(apUser.getId());
        apCommentRepay.setAuthorName(apUser.getName());
        apCommentRepay.setContent(dto.getContent());
        apCommentRepay.setCommentId(dto.getCommentId());
        apCommentRepay.setCreatedTime(new Date());
        apCommentRepay.setUpdatedTime(new Date());
        apCommentRepay.setLikes(0);
        mongoTemplate.save(apCommentRepay);

        /*更新评论的数量*/
        apComment.setReply(apComment.getReply() + 1);
        mongoTemplate.save(apComment);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override/*保存 点赞回复的评论*/
    public ResponseResult saveCommentRepayLike(CommentRepayLikeDto dto) {

        /*检验参数*/
        if (dto.getCommentRepayId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"回复id不存在");
        }

        /*判断回复评论存在不存在*/
        ApCommentRepay apCommentRepay = mongoTemplate.findById(dto.getCommentRepayId(), ApCommentRepay.class);
        if (apCommentRepay==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"回复评论不存在");
        }

        ApUser user = AppThreadLocalUtils.getUser();
        if (user==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"用户不存在");
        }

        /*判重   根据用户id和回复id*/
        List<ApCommentRepayLike> apCommentRepayLikeList = mongoTemplate.find(Query.query(Criteria.where("commentRepayId").is(apCommentRepay.getId()).and("authorId").is(user.getId())), ApCommentRepayLike.class);

        /*如果没查到 就说明没有点赞  就 保存  */
        if (apCommentRepayLikeList.size() == 0) {
            /*没查到 所以 只判断点赞 */
            if (dto.getOperation()==0){

                //更新回复的点赞数量
                apCommentRepay.setLikes(apCommentRepay.getLikes() + 1);
                mongoTemplate.save(apCommentRepay);

                //保存 APP评论信息点赞
                ApCommentRepayLike apCommentLike = new ApCommentRepayLike();
                apCommentLike.setAuthorId(user.getId());
                apCommentLike.setCommentRepayId(apCommentRepay.getId());
                apCommentLike.setOperation(dto.getOperation());
                mongoTemplate.save(apCommentLike);

            }
        }else if (apCommentRepayLikeList.size() == 1) {
            /*查到了就只判断取消点赞*/
            if (dto.getOperation()==1){
                //更新回复的点赞数量
                apCommentRepay.setLikes(apCommentRepay.getLikes()<=0?0:apCommentRepay.getLikes()-1);
                mongoTemplate.save(apCommentRepay);

                /*删除 关于回复评论的点赞表*/
                ApCommentRepayLike apCommentRepayLike = apCommentRepayLikeList.get(0);
                mongoTemplate.remove(apCommentRepayLike);

            }
        }

        //5.数据返回
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("likes",apCommentRepay.getLikes());
        return ResponseResult.okResult(resultMap);
    }
}
